The LCD understands a number of commands which are designed to be used on a range of different configurations of panel. We do not have the time or the space to go into these in any detail right now, although a data sheet for the commands is available for reference. Instead I will just give the commands which we need to use to set the panel up and make it ready for action.
I constructed the commands by creating the bit patterns from the data sheet and then converting them into hex for inclusion in the program.
The function setup_lcd
sends all five bytes of the lcd_init
array and then calls the clear function to clear the display and make it ready for use.
Note also that I set up the hardware in this function, being careful not to change the settings on bits of PORTB which I am not using. I also perform a short delay to let the LCD settle if we are being called just after the power going on.
Load and run Exercise 5.1 and see how it displays a message on the LCD panel.
const unsigned char lcd_init [5] =
{
/* LCD initialize */
0x33,
/* Set for 4 bit operation */
0x32,
/* Set for 2 line LCD */
0x2c,
/* Select move after write */
0x06,
/* disp. on cursor off blink off */
0x0c
} ;
char lcd_start (void)
{
unsigned char i ;
/* set bits of PORTB for output */
/* change for different hardware */
/* clear bottom five bits for LCD*/
/* don't change any other bits */
TRISB = (trtisb & 0xc0) ;
/* give the LCD time to settle */
/* from power up */
lcd_delay ( POWER_UP_DELAY ) ;
for ( i=0 ; i < 5 ; i++ )
{
lcd_command ( lcd_init [i] ) ;
}
lcd_clear () ;
/* say we worked */
return 1 ;
}